/**
 * 初始化企业客户详情对话框
 */
var CustomerCompanyInfoDlg = {
    customerCompanyInfoData: {},
    customerCompanyManageInfoData: {},
    salesmanTreeInstance: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '用户名不能为空'
                }
            }
        },
        shortName: {
            validators: {
                notEmpty: {
                    message: '企业简称不能为空'
                }
            }
        },
        legalPerson: {
            validators: {
                notEmpty: {
                    message: '法定代表人不能为空'
                }
            }
        },
        corporation: {
            validators: {
                notEmpty: {
                    message: '所属集团不能为空'
                }
            }
        },
        militaryCode: {
            validators: {
                notEmpty: {
                    message: '军工代码不能为空'
                }
            }
        },
        areaName: {
            validators: {
                notEmpty: {
                    message: '所属区域不能为空'
                }
            }
        },
        providerNum: {
            validators: {
                digits: {
                    message: '必须是整数'
                }
            }
        },
        departName: {
            validators: {
                notEmpty: {
                    message: '所属部门不能为空'
                }
            }
        },
        customerTypeCode: {
            validators: {
                notEmpty: {
                    message: '客户类型不能为空'
                }
            }
        },
          unifiedSocialCreditCode: {
            validators: {
                notEmpty: {
                    message: '不能为空'
                },
                stringLength: {
                    min: 18,
                    max: 18,
                    message: '请输入18个字符'
                }
            }
        },

        militaryCode: {
            validators: {
                stringLength: {
                    max: 6,
                    message: '请输入最大6个字符'
                }
            }
        },
        annualPurchaseAmount: {
            validators: {
                numeric: {
                    message: '必须为数字'
                }
            }
        },

        annualIncome: {
            validators: {
                numeric: {
                    message: '必须为数字'
                }
            }
        },
        salesmanName: {
            validators: {
                notEmpty: {
                    message: '所属销售不能为空'
                }
            }
        },

        customerNum: {
            validators: {
                digits: {
                    message: '必须是整数'
                }
            }
        },
        mainBusiness:{
            validators: {
                stringLength: {
                    max:200,
                    message: '必须在200个字符以内'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
CustomerCompanyInfoDlg.clearData = function () {
    this.customerCompanyInfoData = {};
    customerCompanyManageInfoData : {
    }
    ;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */

CustomerCompanyInfoDlg.set = function (key, val) {
    this.customerCompanyInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    this.customerCompanyManageInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CustomerCompanyInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
CustomerCompanyInfoDlg.close = function () {
    parent.layer.close(window.parent.CustomerCompany.layerIndex);
}

/**
 * 收集数据
 */
CustomerCompanyInfoDlg.collectData = function () {
    this
        .set('id')
        .set('name')
        .set('unifiedSocialCreditCode')
        .set('shortName')
        .set('legalPerson')
        .set('corporation')
        .set('section')
        .set('militaryCode')
        .set('areaId')
        .set('mainBusiness')
        .set('customerCompanyId')
        .set('customerTypeCode')
        .set('annualIncome')
        .set('customerNum')
        .set('annualPurchaseAmount')
        .set('providerNum')
        .set('departId')
        .set('salesmanId')
        .set('source')
        .set('isCcb')
        .set('isPlanToOpenAccount')
        .set('state');
}

/**
 * 验证数据是否为空
 */
CustomerCompanyInfoDlg.validate = function () {
    $('#customerCompanyForm').data("bootstrapValidator").resetForm();
    $('#customerCompanyForm').bootstrapValidator('validate');
    return $("#customerCompanyForm").data('bootstrapValidator').isValid();
}


/**
 * 提交添加
 */
CustomerCompanyInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/customerCompany/add", function (data) {
        Feng.success("添加成功!");
        window.parent.CustomerCompany.table.refresh();
        CustomerCompanyInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.customerCompanyInfoData);
    ajax.set(this.customerCompanyManageInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
CustomerCompanyInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/customerCompany/update", function (data) {
        Feng.success("修改成功!");
        window.parent.CustomerCompany.table.refresh();
        CustomerCompanyInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.customerCompanyInfoData);
    ajax.set(this.customerCompanyManageInfoData);
    ajax.start();
}

/**
 * 显示部门选择的树
 */
CustomerCompanyInfoDlg.showDeptSelectTree = function () {
    Feng.showInputTree("departName", "deptTypeTreeDiv", 15, 34);

};

/**
 * 选择部门
 * @param e
 * @param treeId
 * @param treeNode
 */
CustomerCompanyInfoDlg.onClickDept = function (e, treeId, treeNode) {
    $("#departName").attr("value", treeNode.name);
    $("#departId").attr("value", treeNode.id);

    //清空销售树
    $("#salesmanName").attr("value", null);
    $("#salesmanId").attr("value", null);
    $("#deptTypeTreeDiv").fadeOut("fast");

}

/**
 * 显示销售员选择的树
 */
CustomerCompanyInfoDlg.showSalesmanSelectTree = function () {
    if (this.salesmanTreeInstance == null && !this.get("departId")) {
        Feng.info("请选择部门!");
    } else {
        CustomerCompanyInfoDlg.initSalesmanSelectTree(this.get("departId"));
        Feng.showInputTree("salesmanName", "salesmanTreeDiv", 15, 34);
    }
}

/**
 * 初始化销售员选择的树
 * @param deptId 部门id
 */
CustomerCompanyInfoDlg.initSalesmanSelectTree = function (deptId) {
    var salesmanTree = new $ZTree("salesmanTree", "/mgr/tree?deptId=" + deptId);
    salesmanTree.bindOnClick(CustomerCompanyInfoDlg.onClickSalesman);
    salesmanTree.init();
    this.salesmanTreeInstance = salesmanTree;
}

/**
 * 选择销售员
 * @param e
 * @param treeId
 * @param treeNode
 */
CustomerCompanyInfoDlg.onClickSalesman = function (e, treeId, treeNode) {
    $("#salesmanName").attr("value", treeNode.name);
    $("#salesmanId").attr("value", treeNode.id);
    $("#salesmanTreeDiv").fadeOut("fast");
}


CustomerCompanyInfoDlg.showAreaSelectTree = function () {
    Feng.showInputTree("areaName", "areaTreeDiv", 15, 34);

};

CustomerCompanyInfoDlg.onClickArea = function (e, treeId, treeNode) {
    $("#areaName").attr("value", treeNode.name);
    $("#areaId").attr("value", treeNode.id);
    $("#areaTreeDiv").fadeOut("fast");
}

$(function () {
    Feng.initValidator("customerCompanyForm", CustomerCompanyInfoDlg.validateFields);


    var deptTree = new $ZTree("deptTree", "/dept/tree");
    deptTree.bindOnClick(CustomerCompanyInfoDlg.onClickDept);
    deptTree.init();


    var areaTree = new $ZTree("areaTree", "/area/tree");
    areaTree.bindOnClick(CustomerCompanyInfoDlg.onClickArea);
    areaTree.init();

});
